//
//  BroadcasterViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/5.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "BroadcasterViewController.h"
#import "BroadcasterTableViewCell.h"
#import "MusicListViewController.h"
#import "PlayerViewController.h"
#import "Player.h"
#import "PlayerModel.h"
#import "MusicModel.h"

@interface BroadcasterViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSDictionary *dataDictionary;
@property(nonatomic,strong)NSArray *broDataArray;
@property(nonatomic,strong)UITableView *broTableView;
//页码
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)BOOL isRefresh;
@end

@implementation BroadcasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self initBroTableView];
}

/**
 *  初始化broTableView
 */
- (void)initBroTableView
{
    self.broTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,64, FM_Screen_Width,FM_Screen_Height-64) style:UITableViewStylePlain];
    self.broTableView.dataSource = self;
    self.broTableView.delegate = self;
    [self.view addSubview:_broTableView];
    
    [self.broTableView registerClass:[BroadcasterTableViewCell class] forCellReuseIdentifier:@"broCell"];
    
    // 发送节目榜单请求
    [self setupRefresh];
}

// UITableViewDataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.broDataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FM_Screen_Pts_Width*70 + FM_Screen_Pts_Height*20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BroadcasterTableViewCell *broCell = [tableView dequeueReusableCellWithIdentifier:@"broCell"];
    broCell.index = indexPath.row+1;
    broCell.topDic = [self.broDataArray objectAtIndex:indexPath.row];
    return broCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 节目
    if(indexPath.section==0)
    {
        NSDictionary *topDic = [self.broDataArray objectAtIndex:indexPath.row];
        NSString *tracksCount = [[topDic objectForKey:@"tracks"] stringValue];
        
        if (tracksCount == nil) {
            // 如果没有集数的则打开播放页
            self.tabBarController.selectedIndex = 2;
            NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iPhone&key=%@&pageId=%ld&pageSize=20",self.contentType,self.key,self.page];
            [self playingFM:indexPath.row requestUrl:url];
        }
        else
        {
            // 如果有集数的则打开fm列表页
            MusicListViewController *musicListVC = [[MusicListViewController alloc] init];
            musicListVC.musicListDic = topDic;
            [self.navigationController pushViewController:musicListVC animated:YES];
        }
    }
    else
    {
        //  主播
#warning 主播
        
        
    }
}

// 播放音频
- (void)playingFM:(NSInteger)index requestUrl:(NSString *)requestUrl
{
    Player *play = [Player shareInstance];
    if (play.playerModel.musicArray.count != 0) {
        [play collectPreseve];
    }
    
    //处理数据源
    PlayerModel *playerModel = [[PlayerModel alloc] init];
    // 专辑序号
    playerModel.index = index;
    // 专辑请求地址
    playerModel.requstUrl = requestUrl;
    // 专辑列表
    playerModel.musicArray = [NSArray arrayWithArray:[MusicModel modelWithArr:[self.dataDictionary objectForKey:@"list"]]];
    for (MusicModel *musicModel in playerModel.musicArray) {
        musicModel.coverLarge = musicModel.coverSmall;
    }
    // 数据源
    play.playerModel = playerModel;
    // 播放
    [play playFromNet];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  网络请求
 */
- (void)getDataWithUrl
{
    if (!self.isRefresh) {
        // 加载更多
        self.page++;
    }else{
        // 刷新
        self.page=1;
    }
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iPhone&key=%@&pageId=%ld&pageSize=20",self.contentType,self.key,self.page];
    [NetWorkManager GET:url isCaChe:NO resultData:^(id responseObject) {
        if (responseObject != nil) {
            self.dataDictionary = responseObject;
             NSArray *tempArr = [responseObject objectForKey:@"list"];
            if (self.page == 1) {
                self.broDataArray = [NSMutableArray array];
            }
            self.broDataArray = [self.broDataArray arrayByAddingObjectsFromArray:tempArr];
            [self.broTableView reloadData];
            [self setupEndRefresh];
        }
        // 移除加载更多
        if ([[responseObject objectForKey:@"maxPageId"] integerValue] == self.page) {
            self.broTableView.footer = nil;
        }else{
            // 尾
            self.broTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                self.isRefresh = NO;
                [self getDataWithUrl];
            }];
        }
    } resultRrror:^(NSError *error) {
        NSLog(@"节目榜单:error,%@",error);
    }];
}

// 设置刷新
- (void)setupRefresh
{
    // 头
    self.broTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = YES;
        [self getDataWithUrl];
    }];
    [self.broTableView.header beginRefreshing];
    
    // 尾
    self.broTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.isRefresh = NO;
        [self getDataWithUrl];
    }];
}

// 结束刷新
- (void)setupEndRefresh
{
    [self.broTableView.header endRefreshing];
    [self.broTableView.footer endRefreshing];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

@end
